﻿using System;
namespace QuickSilver.Career
{
    /// <summary>
    /// 面试题代码集锦
    /// </summary>
    public class CommonInterview
    {
        #region 数组移除相同元素
        
        /// <summary>
        /// 从数组中移除相同的元素
        /// </summary>
        public static void RemoveCommonFromArray()
        {
            int[] ArrayTmp = GetArrayFromConsole();
            if (ArrayTmp.Length == 0)
                return;
            System.Console.WriteLine("Here is the Array before Remove Common");
            PrintArray(ArrayTmp);
            System.Console.WriteLine("Here is the Array after Remove Common");
            PrintArray(RemoveComm(ArrayTmp));
        }
        /// <summary>
        /// 移除数组中相同的项
        /// 该方法假定数组是没有经过排序的
        /// </summary>
        /// <param name="SrcArray"></param>
        /// <returns></returns>
        public static int[] RemoveComm(int[] SrcArray)
        {
            if (SrcArray.Length == 0)
                return SrcArray;

            System.Collections.Generic.List<int> Result = new System.Collections.Generic.List<int>();
            bool HasComm = false;
            for (int i = 0; i < SrcArray.Length-1; i++)
            {
                HasComm = false;
                for (int j = i + 1;j<SrcArray.Length ; j++)
                {
                    if (SrcArray[i] == SrcArray[j])
                    {
                        HasComm = true;
                        break;
                    }
                }
                if (!HasComm)
                {
                    Result.Add(SrcArray[i]);
                }
                if(!HasComm && i==SrcArray.Length-2)
                    Result.Add(SrcArray[SrcArray.Length-1]);
            }
            return ListToArray(Result);
        }
        /// <summary>
        /// 针对已经排序过的数组移除相同的项
        /// </summary>
        /// <param name="SortedArray"></param>
        /// <returns></returns>
        public static int[] RemoveCommSorted(int[] SortedArray)
        {
            if (SortedArray.Length == 0)
                return SortedArray;

            System.Collections.Generic.List<int> Result = new System.Collections.Generic.List<int>();
            for (int i = 0; i < SortedArray.Length - 1; i++)
            {
                if (SortedArray[i]!=SortedArray[i+1])
                {
                    Result.Add(SortedArray[i]);
                    if(i==SortedArray.Length-2)
                    {
                        Result.Add(SortedArray[SortedArray.Length - 1]);
                    }
                }
            }
            return ListToArray(Result);
        }
        #endregion 数组移除相同元素

        #region 排序算法
        public static void QuickSortTest()
        {
            System.Collections.Generic.List<int> List = new System.Collections.Generic.List<int>();
            System.Random rand = new Random();
            for (int i = 0; i < 100; i++)
            {
                List.Add(rand.Next(Int32.MaxValue));
            }
            int[] ArrayTmp = ListToArray(List);
            if (ArrayTmp.Length == 0)
                return;
            System.Console.WriteLine("Here is the Array before Quick Sort");
            PrintArray(ArrayTmp);
            System.Console.WriteLine("\n------------------------------------------------------");
            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            System.Console.WriteLine("Here is the Array after Quick Sort");
            //调用QuickSilver算法类
            QuickSilver.Help.Algorithm.Sort.QuickSort(ref ArrayTmp,0,ArrayTmp.Length-1);
            
            

            stopWatch.Stop();
            PrintArray(ArrayTmp);
            System.Console.WriteLine("\n-----------------------------------------------------");
            System.Console.WriteLine("Times Elapsed:"+stopWatch.Elapsed.Ticks );
        }
        #endregion 排序算法

        #region 其他通用方法
        /// <summary>
        /// 依次输出元素
        /// </summary>
        /// <param name="SrcArray"></param>
        public static void PrintArray(int[] SrcArray)
        {
            if (SrcArray.Length > 0)
            {
                System.Console.WriteLine("Here is the numbers");
                foreach (int tmp in SrcArray)
                {
                    System.Console.Write(tmp + "    ");
                }
            }
            else
            {
                System.Console.WriteLine("Array contains nothing");
            }
        }
        /// <summary>
        /// 将用户在控制台的输入转换为整数数组
        /// </summary>
        public static int[] GetArrayFromConsole()
        {
            System.Console.WriteLine(@"Input numbers, press q or Q to quit!");
            System.Collections.Generic.List<int> TmpList = new System.Collections.Generic.List<int>();
            string ReadKey = System.Console.ReadLine();

            int num;

            while (ReadKey.Trim().ToUpper() != "Q")
            {
                try
                {
                    int.TryParse(ReadKey, out num);
                    TmpList.Add(num);
                }
                catch
                {
                    System.Console.WriteLine("Invalid input");
                }
                finally
                {
                    ReadKey = System.Console.ReadLine();
                }
            }

            return ListToArray(TmpList);

        }
        /// <summary>
        /// 数组转换为列表
        /// </summary>
        /// <param name="SrcArray"></param>
        /// <returns></returns>
        public static System.Collections.Generic.List<int> ArrayToList(int[] SrcArray)
        {
            
            if (SrcArray != null && SrcArray.Length > 0)
            {
                System.Collections.Generic.List<int> ResultList = new System.Collections.Generic.List<int>(SrcArray.Length);

                for (int i = 0; i < SrcArray.Length; i++)
                {
                    ResultList.Add(SrcArray[i]);
                }

                return ResultList;
            }
            else
            {
                return new System.Collections.Generic.List<int>();
            }
        }
        /// <summary>
        /// 列表转换为数组
        /// </summary>
        /// <param name="SrcList"></param>
        /// <returns></returns>
        public static int[] ListToArray(System.Collections.Generic.List<int> SrcList)
        {
            if (SrcList != null && SrcList.Count > 0)
            {
                int[]  ResultArray=new int[SrcList.Count];

                for (int i = 0; i < SrcList.Count; i++)
                {
                   ResultArray[i]=SrcList[i];

                }
                return ResultArray;
            }
            else
            {
                return new int[0];
            }
        }
        #endregion 其他通用方法
    }
}